package admin.mode;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import admin.mode.AdminMode;
import base.Power;
import base.Sql;
import base.tools.Constants;

/**
 * CMS后台DispatchAction基类
 * 
 * @author qsj
 */

public class MemberDispatchAction extends DispatchAction {
	protected Class[] types = { Power.class, AdminMode.class };

	protected Method getMethod(String name) throws NoSuchMethodException {

		synchronized (methods) {
			Method method = (Method) methods.get(name);
			if (method == null) {
				method = clazz.getMethod(name, types);
				methods.put(name, method);
			}
			return (method);
		}

	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=GBK");
		String parameter = mapping.getParameter();
		String name = request.getParameter(parameter);
		if ("execute".equals(name) || "perform".equals(name)) {
			String message = messages.getMessage("dispatch.recursive", mapping
					.getPath());
			// log.error(message);
			throw new ServletException(message);
		}
		return dispatchMethod(mapping, form, request, response, name);

	}

	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {
		if (name == null) {
			return this.unspecified(mapping, form, request, response);
		}
		Method method = null;
		try {
			method = getMethod(name);
		} catch (NoSuchMethodException e) {
			String message = messages.getMessage("dispatch.method", mapping
					.getPath(), name);
			// log.error(message, e);

			String userMsg = messages.getMessage("dispatch.method.user",
					mapping.getPath());
			throw new NoSuchMethodException(userMsg);
		}
		ActionForward forward = null;
		Sql sql = new Sql();
		try {

			Power power = new Power(mapping, form, request, response, sql);
			AdminMode admin = new AdminMode(power);

			Object args[] = { power, admin };
			// 验证登录和权限
			if (admin.checkLogin()
					|| admin.creatGuestSession(power) == AdminMode.LOGIN_SUCCESS) {// 验证登录

				String opt = power.getParameter("opt");
				if (admin.permistionPass(power, opt, admin.getUserInfo())) {// 验证权限
					Date d1 = new Date();
					forward = (ActionForward) method.invoke(this, args);
					Date d2 = new Date();
					long timenum = d2.getTime() - d1.getTime();
					long statnum = sql.getStatNum();
					if (timenum > 500) {// 超过0.5秒钟,记录日至

					}
				} else {
					// 转到无权限提示页

					String url = power.getRequest().getRequestURL().toString()
							+ "?";
					Enumeration param = power.getRequest().getParameterNames();
					while (param.hasMoreElements()) {
						String pname = param.nextElement().toString();
						url += pname + "="
								+ power.getRequest().getParameter(pname) + "&";
					}
					if (url.endsWith("&")) {
						url = url.substring(0, url.lastIndexOf("&"));
					}
					if (url.endsWith("?")) {
						url = url.substring(0, url.lastIndexOf("?"));
					}
					power.getResponse().sendRedirect(
							base.tools.Constants.PREFIX_PATH
									+ "/jsp/login.jsp?url=" + url);
				}
			} else {
				power.setAttribute("message", "登陆错误！");
				return power.findForward("/login.jsp", false);
			}

		} catch (Exception e) {
			response.getWriter().println(
					"<%@ page contentType=\"text/html; charset=GBK\" %>");
			response.getWriter().println("系统错误" + "<!--");
			e.printStackTrace(response.getWriter());
			response.getWriter().println("-->");
		}

		finally {
			sql.close();
		}

		// Return the returned ActionForward instance
		return (forward);
	}
}
